package com.party.core.service.order.biz;

import com.party.common.utils.StringUtils;
import com.party.core.api.order.IOrderDetailService;
import com.party.core.exception.BusinessException;
import com.party.core.model.order.OrderForm;
import com.party.core.model.order.OrderStatus;
import com.party.core.model.order.OrderType;
import com.party.core.model.order.PaymentState;
import com.party.core.service.goods.IGoodsService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.order.IOrderFormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 订单业务处理
 * Created by yifeng
 *
 * @date 2018/2/7 0007
 * @time 下午 15:30
 */
@Service
public class OrderFormBizService {
    @Autowired
    private IOrderFormService orderFormService;
    @Autowired
    private IMemberActService memberActService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderDetailService orderDetailService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 待支付订单超时取消处理
     *
     * @param orderId 订单
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void orderOvertimeCancel(String orderId) {
        try {
            if (StringUtils.isNotEmpty(orderId)) {
                OrderForm orderForm = orderFormService.get(orderId);
                if (null != orderForm) {
                    if (orderForm.getStatus().equals(OrderStatus.ORDER_STATUS_TO_BE_PAID.getCode())) {
                        orderForm.setIsPay(PaymentState.NO_PAY.getCode());
                        orderForm.setStatus(OrderStatus.ORDER_STATUS_OTHER.getCode());
                        orderFormService.update(orderForm);

                        if (orderForm.getType().equals(OrderType.ORDER_ACTIVITY.getCode())
                                || OrderType.ORDER_COURSE.getCode().equals(orderForm.getType())) { // 活动订单
                            // 取消报名
                            memberActService.cancel(orderForm);
                        } else if (orderForm.getType().equals(OrderType.ORDER_MALL.getCode())) { // 商城订单
                            // 释放库存
                            //productBizService.releaseInventory(orderForm.getId());
                            orderDetailService.releaseStock(orderForm.getId());
                        } else if (orderForm.getType().equals(OrderType.ORDER_LEVEL.getCode())) { // 套餐订单

                        } else if (orderForm.getType().equals(OrderType.ORDER_CROWD_FUND.getCode())) { // 众筹订单

                        } else if (OrderType.ORDER_NOMAL.getCode().equals(orderForm.getType()) // 标准玩法 定制玩法
                                || OrderType.ORDER_CUSTOMIZED.getCode().equals(orderForm.getType())) {
                            // 释放库存
                            goodsService.releaseInventory(orderForm);
                        }
                    }
                }
            }
        } catch (BusinessException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("待支付订单超时取消处理异常", e);
        }
    }
}
